package com.yubb.common.utils.bean;

import com.github.dozermapper.core.DozerBeanMapperBuilder;
import com.github.dozermapper.core.Mapper;
import com.yubb.common.exception.BusinessException;
import java.util.*;

/**
 * @Description dozer是一个能把实体与实体之间转换的工具。类似spring的BeanUtils，但功能比BeanUtils强大，性能稍差。<br/>
 *              复杂的用DozerUtils，简单的可以继续用BeanUtils
 * @Author zhushuyong
 * @Date 2021/5/27 23:11
 * @since:
 * @copyright: 版权所有2021 开源组织 gitee(https://gitee.com/jinzheyi)作者：朱述勇<br/>
 * GitHub(https://github.com/jinzheyi)作者：朱述勇 。
 */
public class DozerUtils {

    static Mapper mapper = DozerBeanMapperBuilder.buildDefault();

    /**
     * 对象拷贝
     * @param source 源对象
     * @param targetClass 目标对象
     * @param <T> 泛型
     * @return 目标对象
     */
    public static <T> T copyProperties(Object source, Class<T> targetClass){
        if (Objects.isNull(source)){
            throw new BusinessException("copyProperties对象拷贝源对象为空");
        }
        return mapper.map(source,targetClass);
    }

    /**
     * List转List
     * @param sourceList 数据库查出来的List数组对象 相当于List<Do>
     * @param destinationClass 给前台web页面显示的对象 相当于List<Vo>
     * @param <T> 泛型
     * @return List<Vo>
     */
    public static <T> List<T> copyPropertiesList(Collection sourceList, Class<T> destinationClass) {
        List destinationList = new ArrayList();
        for (Iterator iter = sourceList.iterator(); iter.hasNext();){
            Object sourceObject = iter.next();
            Object destinationObject = mapper.map(sourceObject, destinationClass);
            destinationList.add(destinationObject);
        }
        return destinationList;
    }

}
